package com.kaiwukj.android.ufamily.mvp.ui.page.account.update;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.UserInfoEditUrl)
public class UserinfoEditActivity extends BaseActivity {

    public static final int TEXT = 1;
    public static final int PHONE = 2;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.tv_text_count)
    TextView tvInputCount;

    @Autowired
    String title;
    @Autowired
    String hint;
    @Autowired
    String text;
    @Autowired
    int maxCount;
    @Autowired
    int inputType = TEXT;


    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @OnClick(R.id.btn_submit)
    void submit() {
        String text = etInput.getText().toString().trim();
        if (inputType == PHONE) {
            if (!RegexUtils.isMobileSimple(text)) {
                showMessage("请输入正确的手机号");
                return;
            }
        } else {
            if (StringUtils.isEmpty(text)) {
                showMessage("输入内容不能为空");
                return;
            }
        }
        Intent data = new Intent();
        data.putExtra("extra", text);
        setResult(RESULT_OK, data);
        finish();
    }


    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_userinfo_edit;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText(title);
        etInput.setHint(hint);
        if (!StringUtils.isEmpty(text)) {
            etInput.setText(text);
        }

        etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxCount)});
        if (maxCount == 0) {
            tvInputCount.setVisibility(View.GONE);
        } else {
            tvInputCount.setText(String.format("0/%s", maxCount));
        }
        if (inputType == PHONE) {
            etInput.setInputType(InputType.TYPE_CLASS_PHONE);
        } else {
            etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        }

        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvInputCount.setText(String.format("%s/%s", etInput.getText().toString().trim().length(), maxCount));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
    }
}
